package com.talkweb.cloudbaby.player;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.talkweb.appframework.dialogs.IDialogListener;
import com.talkweb.appframework.log.DLog;

import tv.danmaku.ijk.media.sample.R;


/**
 * Created by tcking on 15/10/27.
 */
public class GiraffePlayerActivity extends FragmentActivity implements IDialogListener {
    private static final String TAG = GiraffePlayerActivity.class.getSimpleName();

    GiraffePlayer player;
    public static final int SYSTEM_UI_FLAG_TRANSPARENT_STATUS_BAR =
            0x00000010;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DLog.i(TAG, "onCreate");
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.giraffe_player);
        int currentPosition = 0;
        if (savedInstanceState != null) {
            currentPosition = savedInstanceState.getInt("currentPosition");
            DLog.i(TAG, "onCreate currentPosition:" + currentPosition + ":" + savedInstanceState.getInt("currentPosition1"));
        }
        Config config = getIntent().getParcelableExtra("config");
        if (config == null || TextUtils.isEmpty(config.url)) {
            Toast.makeText(this, R.string.giraffe_player_url_empty, Toast.LENGTH_SHORT).show();
        } else {
            RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.app_video_box);
            relativeLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            View view = getLayoutInflater().inflate(R.layout.ijk_video_view, null);
            relativeLayout.addView(view, 0, params);
            int playerType=IjkVideoView.playerTypeIjk;
            if (config.playerType == Config.PLAYERTYPE_BOKECC) {
                playerType=IjkVideoView.playerTypeBokeCC;
            }
            player = new GiraffePlayer(this,playerType);
            player.setTitle(config.title);
            player.setDefaultRetryTime(config.defaultRetryTime);
            player.setFullScreenOnly(true);//
            player.setScaleType(TextUtils.isEmpty(config.scaleType) ? GiraffePlayer.SCALETYPE_FITPARENT : config.scaleType);
            player.setTitle(TextUtils.isEmpty(config.title) ? "" : config.title);
            player.setShowNavIcon(config.showNavIcon);
            player.play(config.url);
            if (currentPosition != 0) {
                player.seekTo(currentPosition, true);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        DLog.i(TAG, "onPause");
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        DLog.i(TAG, "onResume");
        if (player != null) {
            player.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DLog.i(TAG, "onDestroy");
        if (player != null) {
            player.onDestroy();
        }
    }

//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//        super.onConfigurationChanged(newConfig);
//        if (player != null) {
//            player.onConfigurationChanged(newConfig);
//        }
//    }

    /**
     * play video
     *
     * @param context
     * @param url     url,title
     */
    public static void play(Activity context, String... url) {
        Intent intent = new Intent(context, GiraffePlayerActivity.class);
        intent.putExtra("url", url[0]);
        if (url.length > 1) {
            intent.putExtra("title", url[1]);
        }
        context.startActivity(intent);
    }

    public static Config configPlayer(Activity activity) {
        return new Config(activity);
    }

    public static class Config implements Parcelable {

        public static final int PLAYERTYPE_BOKECC = 100;
        public static final int PLAYERTYPE_IJK = 101;

        private Activity activity;
        private String scaleType;
        private boolean fullScreenOnly;
        private long defaultRetryTime = 5 * 1000;
        private String title;
        private String url;
        private boolean showNavIcon = true;

        private int playerType;

        public Config setTitle(String title) {
            this.title = title;
            return this;
        }


        public Config(Activity activity) {
            this.activity = activity;
        }

        public void play(String url) {
            this.url = url;
            Intent intent = new Intent(activity, GiraffePlayerActivity.class);
            intent.putExtra("config", this);
            activity.startActivity(intent);
        }

        public Config setDefaultRetryTime(long defaultRetryTime) {
            this.defaultRetryTime = defaultRetryTime;
            return this;
        }

        public Config setScaleType(String scaleType) {
            this.scaleType = scaleType;
            return this;
        }

        public Config setFullScreenOnly(boolean fullScreenOnly) {
            this.fullScreenOnly = fullScreenOnly;
            return this;
        }

        public Config setPlayerType(int playerType) {
            this.playerType = playerType;
            return this;
        }

        private Config(Parcel in) {
            scaleType = in.readString();
            fullScreenOnly = in.readByte() != 0;
            defaultRetryTime = in.readLong();
            title = in.readString();
            url = in.readString();
            showNavIcon = in.readByte() != 0;
            playerType = in.readInt();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(scaleType);
            dest.writeByte((byte) (fullScreenOnly ? 1 : 0));
            dest.writeLong(defaultRetryTime);
            dest.writeString(title);
            dest.writeString(url);
            dest.writeByte((byte) (showNavIcon ? 1 : 0));
            dest.writeInt(playerType);
        }

        public static final Creator<Config> CREATOR = new Creator<Config>() {
            public Config createFromParcel(Parcel in) {
                return new Config(in);
            }

            public Config[] newArray(int size) {
                return new Config[size];
            }
        };
    }

    @Override
    public void onBackPressed() {
//        if (player != null && player.onBackPressed()) {
//            return;
//        }
        super.onBackPressed();
    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        switch (requestCode) {
            case GiraffePlayer.DIALOG_REQUEST_ERROR:
                finish();
                break;
            case GiraffePlayer.DIALOG_REQUEST_MOBILEPLAY:
                player.startPlay();
                break;
            // 播放中弹出 确定使用手机网络播放
            case GiraffePlayer.DIALOG_REQUEST_PLAYING_MOBILE:
                player.startPlay();
                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {
        finish();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            DLog.i(TAG, "onSaveInstanceState:" + player.getCurrentPosition() + ":" + player.getPasueCurrentPosition());
            outState.putInt("currentPosition", player.getPasueCurrentPosition());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        DLog.i(TAG, "onRestoreInstanceState");
        if (savedInstanceState != null) {
            player.setPasueCurrentPosition(savedInstanceState.getInt("currentPosition"));
            DLog.i(TAG, "onRestoreInstanceState:" + savedInstanceState.getInt("currentPosition") + ":" + savedInstanceState.getInt("currentPosition1"));
        }
    }
}
